<?php
/**
 *
 * USe this  to initialie
 *
 *
 *
 */

class AutorisationController extends AppController {

	var $uses = array('Notification','Aros');
	var  $components = array('Acl','Session');

	public function beforeFilter() {
		$this->Auth->authorize = array('Controller');
	}

	public function beforeRender(){
		AppController::beforeRender();
		$this->layout = 'connected';
	}

	public  function isAuthorized(){
		 return $this->isManageAutorisationAllowed();
	}
	

	public function createAcl(){
		$aro = $this->Acl->Aro;
		$aro->create();$aro->save(array('alias'=>'clubSecretaire'));
		$aro->create();$aro->save(array('alias'=>'admin'));
		$aro->create();$aro->save(array('alias'=>'arbitre'));
		$aro->create();$aro->save(array('alias'=>'federalSecretaire'));

		$aco = $this->Acl->Aco;
		$aco->create();$aco->save(array('alias'=>'manageLicence'));
		$aco->create();$aco->save(array('alias'=>'manageAutorisation'));
		$aco->create();$aco->save(array('alias'=>'viewMember'));
		$aco->create();$aco->save(array('alias'=>'manageMember'));


		$this->Acl->allow('clubSecretaire', 'manageLicence');
		$this->Acl->allow('admin', 'admin');

	}
	 
	 
	public function index () {
		$autorisationsRequest = $this->Notification->getAllWaitingAutorisationsRequest();
		$this->set('autorisationsRequest', $autorisationsRequest);

		$clubSecretaires = $this->Aros->getAllUsersForGroup('clubSecretaire');
		$this->set('clubSecretaires',$clubSecretaires);

		$federalSecretaires = $this->Aros->getAllUsersForGroup('federalSecretaire');
		$this->set('federalSecretaires',$federalSecretaires);

		$admins = $this->Aros->getAllUsersForGroup('admin');
		$this->set('admins',$admins);

	    $arbitres = $this->Aros->getAllUsersForGroup('arbitre');
		$this->set('arbitres',$arbitres);
		
	}

	public function valider ($notificationId) {
		$notification = $this->Notification->findById($notificationId);
		$notification['Notification']['status'] = 'ok';
		$notification['Notification']['user_id_responder'] = $this->Session->read('Auth.User.id') ;
		 
		$aro = $this->Acl->Aro;
		$role = $notification['Notification']['request']; // admin, arbitre ...
		$result =  $this->Acl->Aro->findByAlias($role);

		if( $result != null){
			$data = array(
				      'parent_id' => $result['Aro']['id'],
				      'model' => 'User',
				      'foreign_key' => $notification['Notification']['user_id']
			);
			$aro->create();
			$aro->save($data);
			 
			$this->Notification->save($notification);
		}
		$this->redirect('/Autorisation/index');
	}
	 
	public function refuser($notificationId) {
		$notification = $this->Notification->findById($notificationId);
		$notification['Notification']['status'] = 'refuse';
		$notification['Notification']['response'] = 'votre demande a été refusée';
		$notification['Notification']['user_id_responder'] = $this->Session->read('Auth.User.id') ;
		 
		$this->Notification->save($notification);
		$this->redirect('/Autorisation/index');
	}

	 
	 
	public function revoke($userId, $group){
		$aroIds = $this->Aros->getAroIdForUserInGroup($userId,$group);
		$aro = $this->Acl->Aro;
		$aro->delete($aroIds[0]['Aro']['id']);

		$this->redirect('/Autorisation/index');
	}


	public function createRoles(){

		$this->redirect('/Autorisation/index');
	}


}

?>